﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Compilex.Syntatic;
using Compilex.Syntatic.Declarations;

namespace Compilex.CodeGeneration
{
    internal class LibraryFunctions
    {
        private CodeGenerator CodeGenerator { get; set; }
        private ImprimexIntex ImprimexIntex { get; set; }
        private CarreguexIntex CarreguexIntex { get; set; }
        internal const string ParamName = "STDLIB_PARAM";
        static bool globalVariableBeenSet = false;
        static string globalVariableLabel;
        internal LibraryFunctions(CodeGenerator generator)
        {
            this.CodeGenerator = generator;
            this.ImprimexIntex = new ImprimexIntex(CodeGenerator);
            this.CarreguexIntex = new CarreguexIntex(CodeGenerator);
            if (!globalVariableBeenSet)
            {
                globalVariableLabel = this.CodeGenerator.AddGlobalVariable(ParamName, new SymbolType(TypeValue.intex));
                globalVariableBeenSet = true;
            }
        }

        internal void DoCall(string functionName)
        {
            if (functionName == "imprimex_intex")
            {
                ImprimexIntex.Invoke();
                return;
            }
            if (functionName == "carreguex_intex")
            {
                
                CarreguexIntex.Invoke();
                return;
            }
            
            throw new InternalSyntaticException("Função padrão não reconhecida: " + functionName);
        }

        internal void PushLibraryFunctionArgument(string argumentLabel)
        {
            CodeGenerator.Ops.Move(globalVariableLabel, argumentLabel);
        }
    }
}
